<?php

/**
 * Copyright 2008 William J. Szerdy Jr.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Bill Szerdy <bszerdy@gmail.com>
 *
 * 12.05.2008 - bszerdy : Created.
 *
 */

/**
 * Saves the blinds from the config-blinds.php response.
 *
 * @param array $response
 *
 * @todo - When a blind is deleted the level numbers need to shift.
 * 
 */
function save_layout_blinds($response) {
	/* Loop through the form data and pull the blind info */
    $level = 1;
    $new_data = array();

    $blind_count = count($response);

    foreach ($response as $blind) {
        $b = new SingleBlind($level,
            $blind->small_blind,
            $blind->big_blind,
            $blind->ante,
            $blind->time);
        $new_data[$level] = $b;
		$level++;
    }

    Data::Save_Data($new_data, TRUE);

	// Reload the data to propigate the changes
	$_SESSION['blinds'] = Data::Load_Data();
}

/**
 * Returns an HTML string formatted for the config-blinds.php
 * 
 * @return string
 */
function layout_blinds($add = 0) {
	$blinds = $_SESSION['blinds'];
	
	$level = 1;
    $retval = "<input type=\"hidden\" id=\"blind_count\" name=\"blind_count\" value=\"" .count($blinds). "\" />\n";
	$retval .= "<div id='blind_layout'>\n";
	
	foreach ($blinds as $blind) {
        $temp_time = get_minutes_and_seconds($blind->time);
		$retval .= <<<HEREGO
<div class='node' id='level_{$level}' name='level_{$level}'>
    <h3>Level {$blind->level}: </h3>
    <table>
        <tr><td><label for='small_blind_{$level}'>Small Blind:&nbsp;</label></td>
            <td><input type='text' id='small_blind_{$level}' name='small_blind_{$level}' value='{$blind->small_blind}' size='5' /></td></tr>
        <tr><td><label for='big_blind_{$level}'>Big Blind:&nbsp;</label></td>
            <td><input type='text' id='big_blind_{$level}' name='big_blind_{$level}' value='{$blind->big_blind}' size='5' /></td></tr>
        <tr><td><label for='ante_{$level}'>Ante:&nbsp;</label></td>
            <td><input type='text' id='ante_{$level}' name='ante_{$level}' value='{$blind->ante}' size='5' /></td></tr>
        <tr><td><label for='length_{$level}'>Blind Length:&nbsp;</label></td>
            <td><input type='text' id='time_{$level}' name='time_{$level}' value='{$temp_time}' size='5' /></td></tr>
        <tr><td><input type='checkbox' id='delete_{$blind->level}' name='delete_{$blind->level}' />Remove</td>
            <td></td></tr>
    </table>
</div>
HEREGO;
        $level++;
    }
	for ($i = 0; $i < $add ; $i++) {
		$retval .= <<<ADD
<div class='node' id='level_{$level}' name='level_{$level}'>
    <h3>Level {$level}: </h3>
    <table>
        <tr><td><label for='small_blind_{$level}'>Small Blind:&nbsp;</label></td>
            <td><input type='text' id='small_blind_{$level}' name='small_blind_{$level}' value='' size='5' /></td></tr>
        <tr><td><label for='big_blind_{$level}'>Big Blind:&nbsp;</label></td>
            <td><input type='text' id='big_blind_{$level}' name='big_blind_{$level}' value='' size='5' /></td></tr>
        <tr><td><label for='ante_{$level}'>Ante:&nbsp;</label></td>
            <td><input type='text' id='ante_{$level}' name='ante_{$level}' value='' size='5' /></td></tr>
        <tr><td><label for='length_{$level}'>Blind Length:&nbsp;</label></td>
            <td><input type='text' id='time_{$level}' name='time_{$level}' value='' size='5' /></td></tr>
    </table>
</div>
ADD;
		$level++;
	}
    $retval .= "</div>\n";
	return $retval;
}

/**
 * Returns an array of SingleBlinds
 * 
 * @param array $post
 * @return array of SingleBlind
 */
function blinds_from_response($post) {
	if (!isset($post['blind_count'])) {
		die('malformed response, "blind_count" no found.');
	}
	
	$retval = array();
	$count = $post['blind_count'] + 1;

	for ($i = 1; $i <= $count; $i++) {
		if (!array_key_exists("small_blind_{$i}", $post)) {
			continue;
		}

		if ((array_key_exists("delete_{$i}", $post)) && ($post["delete_{$i}"] == 'on')) {
            continue;
        }
		
		$b = new SingleBlind(
			$i,
			$post["small_blind_{$i}"],
			$post["big_blind_{$i}"],
			$post["ante_{$i}"],
			get_seconds($post["time_{$i}"])
		);
		array_unshift($retval, $b);
	}

	$retval = array_reverse($retval);

	// Sort the array
	usort($retval, array("SingleBlind", "CompareBlinds"));
	
	return $retval;
}

/**
 * Returns a single blind for the requested level
 *
 * @param integer $level
 * @return SingleBlind
 */
function get_blind($level) {
	if ($level == null)
		$level = 1;
    
	foreach ($array as $blind) {
		if ($blind->level == $level) {
            $b = new SingleBlind(
				$blind->level,
                $blind->small_blind,
                $blind->big_blind,
                $blind->ante,
                $blind->time);
            
        }
		return $b;
	}
	
	return 0;
}

/**
 * Returns the number of seconds from a mm:ss formatted string
 *
 * @param string $time
 */
function get_seconds($time) {
	$t = explode(':', $time);
    $total = 0;

    /* Minutes */
	if (isset($t[0])) {
		if ($t[0] != "00") {
			$total += intval($t[0]) * 60;
		}

		if (count($t[0] < 2)) {
			str_pad($t[0], 2, '0', STR_PAD_LEFT);
		}
	}

	/* Seconds */
	if (isset($t[1])) {
		if ($t[1] != "00") {
			$total += $t[1];
		}

		if (count($t[1] < 2)) {
			str_pad($t[1], 2, '0', STR_PAD_LEFT);
		}
	}
	
    return $total;
}

/**
 * Returns a string representation (mm:ss) from the number
 *  of seconds passed in.
 *
 * @param integer $seconds
 * @return string
 */
function get_minutes_and_seconds($seconds) {
	return sprintf( "%02.2d:%02.2d", floor( $seconds / 60 ), $seconds % 60 );

    $hms = "";
    $minutes = floor($seconds / 60);
    $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
    $secs = intval($seconds % 60);
    $hms .= str_pad($secs, 2, "0", STR_PAD_LEFT);

    return $hms;
}

?>